package h0802;

import java.util.Scanner;

public class MaxSubmatrixSum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // 读取输入矩阵的尺寸
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        
        // 读取矩阵
        int[][] matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }
        
        // 计算和最大子矩阵的和
        int result = maxSubmatrixSum(matrix, n, m);
        System.out.println(result);
    }
    
    private static int maxSubmatrixSum(int[][] matrix, int n, int m) {
        // 计算前缀和
        int[][] prefixSum = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                prefixSum[i][j] = matrix[i - 1][j - 1] 
                                + prefixSum[i - 1][j] 
                                + prefixSum[i][j - 1] 
                                - prefixSum[i - 1][j - 1];
            }
        }
        
        int maxSum = Integer.MIN_VALUE;
        
        // 枚举上下边界
        for (int top = 1; top <= n; top++) {
            for (int bottom = top; bottom <= n; bottom++) {
                // 使用Kadane算法在每一列上求最大子数组和
                int currentSum = 0;
                for (int col = 1; col <= m; col++) {
                    int colSum = getSum(prefixSum, top, col, bottom, col);
                    currentSum += colSum;
                    if (currentSum > maxSum) {
                        maxSum = currentSum;
                    }
                    if (currentSum < 0) {
                        currentSum = 0;
                    }
                }
            }
        }
        
        return maxSum;
    }
    
    private static int getSum(int[][] prefixSum, int x1, int y1, int x2, int y2) {
        return prefixSum[x2][y2] 
             - prefixSum[x1 - 1][y2] 
             - prefixSum[x2][y1 - 1] 
             + prefixSum[x1 - 1][y1 - 1];
    }
}
